""" 将三部分的数据文件（图片和标签）挪动到对应目录"""
import os
import shutil
import traceback


def moveFile(file, targetDir):
    try:
        if not os.path.exists(targetDir):
            os.mkdir(targetDir)
        shutil.move(file, targetDir)
    except Exception as e:
        print("move_file ERROR: ", e)
        traceback.print_exc()


if __name__ == "__main__":
    filePath = r"/run/media/kearney/a/CAU/42course/毕设/datasets/NEU-DET/"

    imgPath = r"/run/media/kearney/a/CAU/42course/毕设/datasets/NEU-DET/images"
    labPath = r"/run/media/kearney/a/CAU/42course/毕设/datasets/NEU-DET/labels"
    allFiles = ["train", "val", "test"]
    for fdata in allFiles:
        fileFullPath = os.path.join(filePath, (fdata + ".txt"))
        print(fdata, fileFullPath)
        with open(fileFullPath, "r") as f:
            for line in f.readlines():
                line = line.strip("\n")

                moveFile(
                    os.path.join(imgPath, (line + ".jpg")), os.path.join(imgPath, fdata)
                )
                moveFile(
                    os.path.join(labPath, (line + ".txt")), os.path.join(labPath, fdata)
                )
